import pandas as pd
from utils.read_url import get_url
from config import config


def _parse_html(html_soup):
    arr = []
    soup_list = html_soup.find_all('div', class_='left-box')
    for soup in soup_list:
        product_name = soup.find('h1', class_='ellipsis').get('title')
        sale_object = None
        sale_info = soup.find('div', class_='customer_box')
        if sale_info:
            sale_object = sale_info.get_text().strip() if sale_info.get_text() else None
        buy_amount = soup.find_all('span', class_='nb')[0].get_text() if soup.find_all('span', class_='nb') else None
        risk_level = soup.find_all('span', class_='nb')[1].get_text() if len(soup.find_all('span', class_='nb')) >= 2 \
            else None
        pre_benifit = soup.find_all('span', class_='nb')[2].get_text() if len(soup.find_all('span', class_='nb')) >= 3 \
            else None
        pre_benifit = pre_benifit.replace('%', '').strip() if pre_benifit else None
        pre_benifit = float(pre_benifit) if pre_benifit else None
        if sale_object != '特殊客群':
            arr.append([product_name, sale_object, buy_amount, risk_level, pre_benifit])

    return arr


def read_main():
    df = pd.DataFrame()
    html_soup = get_url(url=config.URL)
    if html_soup:
        arr = _parse_html(html_soup)
        if arr:
            df = pd.DataFrame(arr)
            if len(arr) == 1:
                df = df.T
            df.columns = ['product_name', 'sale_object', 'buy_amount', 'risk_level', 'pre_benifit']
            df = df[df['pre_benifit'] > 0]
            df = df.sort_values('pre_benifit', ascending=False)
    return df

df = read_main()
print(df)
print(len(df))
